﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using SYcwpb.Helpers;

namespace SYcwpb.WinForm.JCT
{
    public partial class EmptyTrainInputFrm : Form
    {
        SqlDataLayer sqlHelper = new SqlDataLayer();

        public EmptyTrainInputFrm(string tbnamein,JCTFrm mainfrm)
        {
            mainFrm = mainfrm;
            tbname = tbnamein;
            InitializeComponent();
            this.groupBox1.Text = tbname + this.groupBox1.Text;
            this.groupBox2.Text = tbname + this.groupBox2.Text;
        }
        public ArrayList checkarr = new ArrayList();
        public DataTable savedEmptytr;
        public JCTFrm mainFrm;
        public ArrayList etTrainArray = new ArrayList();
        public string tbname = "";
        private void EmptyTrainInputFrm_Load(object sender, EventArgs e)
        {
            checkarr.Clear();
            string sql = "select distinct TrainID,MidID,LastID from ["+tbname+"] order by MidID ASC,LastID ASC";
            DataTable dt = sqlHelper.SelectDt(sql);
            
            sql = "select * from [EmptyTrain] where ttbname='" + tbname + "'";
            savedEmptytr = sqlHelper.SelectDt(sql);

            for (int i = 0; i < dt.Rows.Count;i++ )
            {
                ListViewItem item = new ListViewItem(dt.Rows[i][0].ToString());
                this.lv_1.Items.Add(item);
            }
            for (int i = 0; i < savedEmptytr.Rows.Count; i++)
            {
                ListViewItem item = new ListViewItem(savedEmptytr.Rows[i]["emptytr"].ToString());
                this.lv_2.Items.Add(item);
                checkarr.Add(savedEmptytr.Rows[i]["emptytr"].ToString());
            }
        }

        private void lv_2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btn_ok_Click(object sender, EventArgs e)
        {

            string sql = "delete from [EmptyTrain] where ttbname='" + tbname + "'";
            if (sqlHelper.ExecuteSql(sql) < 0)
            {
                MessageBox.Show("空载车更新前的删除失败！");
                return;
            }
            foreach (ListViewItem lv2item in this.lv_2.Items)
            {
                string strlv2item = lv2item.SubItems[0].Text.ToString();
                sql = "insert into [EmptyTrain] (ttbname,emptytr) values ('" + tbname + "','" + strlv2item + "')";
                etTrainArray.Add(strlv2item);
                if (sqlHelper.ExecuteSql(sql) < 0)
                {
                    MessageBox.Show("空载车次保存失败！");
                }
            }
            mainFrm.etTrainArray = this.etTrainArray;
            this.DialogResult = DialogResult.OK;
            this.Dispose();
        }

        private void btn_add_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem lv1item in this.lv_1.SelectedItems)
            {
                string strlv1item = lv1item.SubItems[0].Text.ToString();
                if (!checkarr.Contains(strlv1item))
                {
                    ListViewItem lv2item = new ListViewItem(strlv1item);
                    this.lv_2.Items.Add(lv2item);
                    checkarr.Add(strlv1item);
                }

            }
        }

        private void btn_delete_Click(object sender, EventArgs e)
        {
           foreach (ListViewItem lv2item in this.lv_2.SelectedItems)
           {
               this.lv_2.Items.Remove(lv2item);
               checkarr.Remove(lv2item);
           }
        }
    }
}
